<template>
  <div class="Appointment">
    <el-card>
      <div class="one">
        <el-date-picker
          v-model="dataTimeVal"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          value-format="yyyy-MM-dd"
          @change="focus"
        >
        </el-date-picker>
        <div class="tow_txt">发布人ID</div>
        <el-input
          placeholder="请输入ID"
          v-model="id"
          oninput="value=value.replace(/[^\d]/g,'')"
        >
        </el-input>

        <div class="tow_txt">发布人性别</div>
        <el-select v-model="sex" placeholder="请选择">
          <el-option label="不限" value=""> </el-option>
          <el-option label="男" value="1"> </el-option>
          <el-option label="女" value="2"> </el-option>
        </el-select>
        <div class="tow_txt">推荐状态</div>
        <el-select v-model="state" placeholder="请选择">
          <el-option label="不限" value=""> </el-option>
          <el-option label="未推荐" value="0"> </el-option>
          <el-option label="已推荐" value="1"> </el-option>
        </el-select>
        <!-- <div class="tow_txt">预约内容</div>
        <el-select v-model="contentType" placeholder="请选择">
          <el-option label="不限" value=""> </el-option>
          <el-option label="视频" value="1"> </el-option>
          <el-option label="图片" value="2"> </el-option>
        </el-select> -->
        <el-button class="btn" @click="search">搜索</el-button>
      </div>
      <!-- <div style="margin-top: 20px; margin-bottom: 20px">
        <el-radio-group v-model="type" size="medium" fill="#6490d5">
          <el-radio-button label="1">未审核</el-radio-button>
          <el-radio-button label="2">已审核</el-radio-button>
        </el-radio-group>
      </div> -->
      <el-table
        :data="list"
        style="width: 100%"
        v-loading="loading"
        @selection-change="handleSelectionChange"
        tooltip-effect="dark"
        ref="multipleTable"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="release_name" label="发布人"> </el-table-column>
        <el-table-column prop="release_pro_id" label="发布人ID">
        </el-table-column>
        <el-table-column prop="sex" label="性别"> </el-table-column>
        <el-table-column prop="userAuth" label="实名认证"> </el-table-column>
        <el-table-column prop="create_time" label="发布时间"> </el-table-column>
        <el-table-column prop="title" label="标题"> </el-table-column>
        <el-table-column prop="photos" label="内容">
          <template slot-scope="scope">
            <div class="box1" v-if="scope.row.photos.length != 0">
              <el-badge
                :value="scope.row.photos.length"
                class="item"
                type="info"
              >
                <el-image
                  style="width: 80px; height: 100px"
                  :src="scope.row.photos[0]"
                  :preview-src-list="scope.row.photos"
                >
                </el-image>
              </el-badge>
            </div>
            <div v-if="scope.row.video.length != 0">
              <!-- {{ scope.row.video }} -->
              <a
                class="el-icon-video-camera link"
                :href="scope.row.video"
                target="_blank"
              >
              </a>
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="video" label="视频"></el-table-column>
        <el-table-column prop="voice_url" label="语音"></el-table-column> -->
        <el-table-column prop="price" label="价格（秒）"> </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button
              type="info"
              plain
              size="small "
              @click="colse(scope.row.id)"
              >关闭</el-button
            >
            <el-button
              type="success"
              v-if="scope.row.recommended == 0"
              plain
              size="small "
              @click="recommended('1', scope.row.id)"
              >推荐</el-button
            >
            <el-button
              type="danger"
              v-else
              plain
              size="small "
              @click="recommended('0', scope.row.id)"
              >取消推荐</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-top: 20px">
        <el-button @click="BatchRecommendation">批量推荐</el-button>
        <el-button @click="toggleSelection()">取消选择</el-button>
      </div>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="page"
        :limit.sync="pageSize"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination"; // 分页
import { adminEngageList, adminEngageClose } from "@/api/AuditManagement";
export default {
  components: { Pagination },
  data() {
    return {
      loading: true,
      ids1: [],
      ids: [],
      multipleSelection: [],
      id: "",
      dataTimeVal: "", //时间日期
      dataTimeValStartDate: "",
      dataTimeValEndDate: "",
      pickerOptions: {
        disabledDate(platformValue) {
          return platformValue.getTime() > Date.now() - 8.64e6;
        },
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 6);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      sex: "",
      state: "",
      contentType: "",
      type: "1",
      list: [],
      total: 0,
      page: 1,
      pageSize: 10,
    };
  },
  methods: {
    // 批量推荐
    async BatchRecommendation() {
      if (this.ids1.length >= 1) {
        const res1 = await adminEngageClose({
          type: "1",
          recommended: "1",
          id: this.ids1,
        });
        if (res1.code == 0) {
          this.$message({
            type: "success",
            message: "批量推荐成功!",
          });
          this.getList();
        }
      } else {
        this.$message.error("请选择未推荐的用户");
      }
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      console.log(val);

      this.ids = val.filter((item) => {
        if (item.recommended == "1") {
          return false;
        } else {
          return item.id;
        }
      });
      this.ids1 = this.ids.map((item) => item.id);
      console.log(this.ids1);
    },

    //时间
    focus(time) {
      if (!this.dataTimeVal) {
        this.dataTimeVal = [];
      }
      this.dataTimeValStartDate = this.dataTimeVal[0];
      this.dataTimeValEndDate = this.dataTimeVal[1];
    },
    search() {
      this.page = 1;
      this.getList();
    },
    async getList() {
      this.loading = true;
      const res = await adminEngageList({
        page: this.page,
        pageSize: this.pageSize,
        startTime: this.dataTimeValStartDate,
        endTime: this.dataTimeValEndDate,
        user_id: this.id,
        sex: this.sex,
        recommended: this.state,
      });
      console.log(res);
      if (res.code == 0) {
        // console.log(res);
        this.list = res.data.list;
        this.total = parseInt(res.data.total);
        this.loading = false;
      }
    },
    //关闭
    colse(id) {
      this.$confirm("此操作将关闭该预约, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // this.$message({
          //   type: 'success',
          //   message: '删除成功!'
          // });
          const res = await adminEngageClose({
            type: "2",
            id,
          });
          if (res.code == 0) {
            this.$message({
              type: "success",
              message: "关闭成功!",
            });
            this.getList();
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消关闭",
          });
        });
    },
    async recommended(recommended, id) {
      // console.log([id]);
      const res = await adminEngageClose({
        type: "1",
        recommended,
        id: [id],
      });
      if (res.code == 0) {
        this.$message({
          type: "success",
          message: "操作成功!",
        });
        this.getList();
      }
    },
  },
  created() {
    this.getList();
  },
};
</script>

<style lang='less' scoped>
.Appointment {
  margin: 30px;
  height: 100%;
  .one {
    display: flex;
    margin-bottom: 20px;
    .el-date-editor {
      margin-right: 20px;
    }
    .el-input {
      width: 150px;
      margin: 0 20px;
    }
    .el-button {
      height: 40px;
    }
    align-items: center;
    .tow_txt {
      line-height: 40px;
      height: 40px;
      color: #9d9fa8;
    }
    .el-select {
      width: 150px;
      margin: 0 20px 0 10px;
    }
  }
  .box1 {
    padding-top: 10px;
  }
  .link {
    display: inline-block;
    width: 80px;
    height: 100px;
    border: 1px dashed #ccc;
    font-size: 50px;
    line-height: 100px;
  }
}
</style>